package com.gxk.stage;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.gxk.res.FontFactory;
import com.gxk.res.ImageFactory;
import com.gxk.res.PublicConst;

public class GameoverStage extends Stage {

	private Image gameOverImage;
	private Image scorePanelImage;
	private Image newImg;
	private Image medalImg;

	private BitmapFont font;
	private Batch batch;

	private float speed;

	private float scorePanelX;
	private float scorePanelY;

	public GameoverStage() {
		this.create();
		this.stageInit();
		this.addStageActor();
	}

	private void create() {
		this.gameOverImage = ImageFactory.getOverImage();
		this.scorePanelImage = ImageFactory.getScorePanelImage();
		this.newImg = ImageFactory.getNewImage();
		this.medalImg = ImageFactory.getMedalImage(0);

		this.font = FontFactory.getFont24();
		this.batch = this.getBatch();
	}

	private void stageInit() {
		this.gameOverImage.setPosition(55, 350);
		this.scorePanelImage.setPosition(25, -126);

		this.speed = 4f;
		this.scorePanelX = 25;
		this.scorePanelY = 0;
	}

	private void addStageActor() {
		this.addActor(gameOverImage);
		this.addActor(scorePanelImage);
		this.addActor(medalImg);
	}

	@Override
	public void act() {

		if (PublicConst.isDefeat) {
			this.addActor(newImg);
		} else {
			newImg.remove();
		}

		this.medalImg.remove();
		if (PublicConst.currentScore < 10) {
			this.medalImg = ImageFactory.getMedalImage(3);
		} else if (PublicConst.currentScore < 30) {
			this.medalImg = ImageFactory.getMedalImage(2);
		} else if (PublicConst.currentScore < 70) {
			this.medalImg = ImageFactory.getMedalImage(1);
		} else {
			this.medalImg = ImageFactory.getMedalImage(0);
		}
		this.addActor(this.medalImg);

		this.scorePanelY += speed;
		if (this.scorePanelY > 200) {
			this.scorePanelY = 200;
		}

		this.scorePanelImage.setPosition(this.scorePanelX, this.scorePanelY);
		this.newImg.setPosition(this.scorePanelX + 140, this.scorePanelY + 72);
		this.medalImg.setPosition(this.scorePanelX + 35, this.scorePanelY + 38);
	}

	public void reStart() {
		PublicConst.isDefeat = false;
		this.scorePanelY = -126;
	}

	@Override
	public void draw() {
		super.draw();

		batch.begin();

		float x = 0;
		float y = this.scorePanelY + 85;
		if (PublicConst.currentScore < 10) {
			x = scorePanelX + 190;
		} else if (PublicConst.currentScore < 100) {
			x = scorePanelX + 174;
		} else {
			x = scorePanelX + 160;
		}

		font.draw(batch, Integer.toString(PublicConst.currentScore), x, y);

		if (PublicConst.maxScore < 10) {
			x = scorePanelX + 190;
		} else if (PublicConst.maxScore < 100) {
			x = scorePanelX + 174;
		} else {
			x = scorePanelX + 160;
		}

		font.draw(batch, Integer.toString(PublicConst.maxScore), x, y - 45);

		batch.end();
	}
}
